package com.platform.controller;

import com.platform.entity.MerchantEntity;
import com.platform.entity.MerchantInfoEntity;
import com.platform.entity.MerchantInfomationEntity;
import com.platform.entity.UserAssetEntity;
import com.platform.service.*;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("merchantassets")
public class MerchantAssetsController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private UserAssetService userAssetService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MerchantInfomationService merchantInfomationService;
    @Autowired
    private MerchantInfoService merchantInfoService;

    @RequestMapping("list")
    public R list(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        List<Map<String,Object>> queryList = merchantInfomationService.queryListByParam(query);
        Integer queryTotal = merchantInfomationService.queryTotal(query);
        PageUtils pageUtils = new PageUtils(queryList,queryTotal,query.getLimit(),query.getPage());
        for (Map<String,Object> map : queryList){
                map.put("id",Integer.parseInt(map.get("mid").toString()));

                map.put("amount",map.get("amount").toString());
                map.put("frozen",map.get("frozen").toString());
                map.put("merchantName",map.get("name").toString());
                //MerchantInfomationEntity merchantInfomationEntity = merchantInfomationService.queryObjectByMid(Integer.parseInt(map.get("mid").toString()));

                MerchantEntity merchantEntity = merchantService.queryObject(Integer.parseInt(map.get("mid").toString()));
               if(merchantEntity!=null){
                   Map<String,Object> cashOutMap = cashOutService.queryCashOutMap(merchantEntity.getUserId().intValue());
                   map.put("cashOutTotal",cashOutMap.get("totalCashOut").toString());
                   map.put("cashOutTotalPrice",cashOutMap.get("totalCashOutPrice").toString());
                   Map<String,Object> orderMap = orderService.queryOrderMap(merchantEntity.getId());
                   map.put("orderTotal",orderMap.get("orderTotal").toString());
                   map.put("orderTotalPrice",orderMap.get("orderTotalPrice").toString());
               }
        }
        return R.ok().put("page", pageUtils);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("merchant:info")
    public R info(@PathVariable("id") Integer id) {
        MerchantEntity merchant = merchantService.queryObject(id);
        Map<String,Object> map = new HashMap<String, Object>();

        MerchantInfomationEntity merchantInfomationEntity = merchantInfomationService.queryObjectByMid(id);
        UserAssetEntity assetEntity = userAssetService.queryObjectForUid(merchant.getUserId());
        map.put("id",id);
        map.put("amount",merchantInfomationEntity.getAmount());
        map.put("frozen",merchantInfomationEntity.getFrozen());
//        map.put("coin",assetEntity.getCoin());
//        map.put("coin_frozen",assetEntity.getCoinFrozen());
        map.put("merchantName",merchant.getName());

        Map<String,Object> cashOutMap = cashOutService.queryCashOutMap(assetEntity.getUid().intValue());
        map.put("cashOutTotal",cashOutMap.get("totalCashOut").toString());
        map.put("cashOutTotalPrice",cashOutMap.get("totalCashOutPrice").toString());

        Map<String,Object> orderMap = orderService.queryOrderMap(id);
        map.put("orderTotal",orderMap.get("orderTotal").toString());
        map.put("orderTotalPrice",orderMap.get("orderTotalPrice").toString());
        return R.ok().put("merchantassets", map);
    }
}
